﻿namespace com.vs.Config.BL
{
    using com.vs.DataAccess;
    using com.vs.DataAccess.DO;
    using com.vs.General;
    using System;
    using System.Data;

    public class ConfigHelper
    {
        private static ConfigHelper _helper = null;

        public string DeleteFilterDetail(Session sess, int iObjID)
        {
            FilterConfigDetailDO modelByPK = null;
            string str = null;
            try
            {
                modelByPK = FilterConfigDetailDO.GetModelByPK(sess, iObjID);
                if (modelByPK != null)
                {
                    str = this.DeleteFilterOrderBy(sess, modelByPK.RECORD_TYPE, modelByPK.FILTER_NAME);
                    if (!StringManager.IsEmpty(str))
                    {
                        return str;
                    }
                    str = this.DeleteFilterStatements(sess, modelByPK.RECORD_TYPE, modelByPK.FILTER_NAME);
                    if (!StringManager.IsEmpty(str))
                    {
                        return str;
                    }
                    modelByPK.Delete(sess);
                }
                return null;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                return (MessageManager.MSG_GEN_000 + exception.Message);
            }
        }

        public string DeleteFilterGroupBy(Session sess, string stRecordType, string stFilterName)
        {
            DataHelper helper = null;
            string str;
            try
            {
                if (StringManager.IsEmpty(stRecordType) || StringManager.IsEmpty(stFilterName))
                {
                    return null;
                }
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, "CFG_DFilterGroupBy", new IDataParameter[] { DataHelper.BuildParameter(sess, "@RECORD_TYPE", stRecordType), DataHelper.BuildParameter(sess, "@FILTER_NAME", stFilterName) });
                str = null;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                str = MessageManager.MSG_GEN_000 + exception.Message;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return str;
        }

        public string DeleteFilterOrderBy(Session sess, string stRecordType, string stFilterName)
        {
            DataHelper helper = null;
            string str;
            try
            {
                if (StringManager.IsEmpty(stRecordType) || StringManager.IsEmpty(stFilterName))
                {
                    return null;
                }
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, "CFG_DFilterOrderBy", new IDataParameter[] { DataHelper.BuildParameter(sess, "@RECORD_TYPE", stRecordType), DataHelper.BuildParameter(sess, "@FILTER_NAME", stFilterName) });
                str = null;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                str = MessageManager.MSG_GEN_000 + exception.Message;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return str;
        }

        public string DeleteFilterStatements(Session sess, string stRecordType, string stFilterName)
        {
            DataHelper helper = null;
            string str;
            try
            {
                if (StringManager.IsEmpty(stRecordType) || StringManager.IsEmpty(stFilterName))
                {
                    return null;
                }
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, "CFG_DFilterStatement", new IDataParameter[] { DataHelper.BuildParameter(sess, "@RECORD_TYPE", stRecordType), DataHelper.BuildParameter(sess, "@FILTER_NAME", stFilterName) });
                str = null;
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                str = MessageManager.MSG_GEN_000 + exception.Message;
            }
            finally
            {
                if (helper != null)
                {
                    helper.Dispose();
                }
            }
            return str;
        }

        public static ConfigHelper GetInstance()
        {
            if (_helper == null)
            {
                _helper = new ConfigHelper();
            }
            return _helper;
        }

        public string SaveGenericConfigDetail(Session sess, GenericConfigDetailDO cfgDO)
        {
            try
            {
                if (cfgDO.OBJECT_ID > 0.0)
                {
                    cfgDO.Update(sess);
                    return (cfgDO.OBJECT_ID.ToString());
                }
                if (GenericConfigDetailDO.GetModelByPK(sess, cfgDO.RECORD_TYPE, cfgDO.IDENTIFIER) != null)
                {
                    return MessageManager.MSG_CFG_001;
                }
                return cfgDO.Insert(sess);
            }
            catch (Exception exception)
            {
                ExceptionManager.LogException(sess, exception);
                return MessageManager.MSG_GEN_000;
            }
        }
    }
}

